// pages/integral/integral-dhInfo.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {

    oid: ''


  },

  goIndex(e) {
    wx.switchTab({
      url: '../index/index',
    })
  },

  bindConfirm(e) {
    const that = this;
    let ercode = that.getScene(that.options.scene);
    wx.showModal({
      cancelText: '想一想',
      confirmText: '确定',
      content: '核销完成无法撤销!',
      showCancel: true,
      title: '确认核销?',
      success: (res) => {
        if (res.confirm == true) {
          wx.showLoading({
            title: '玩命加载中',
            mask: true,
          })
          wx.request({
            url: app.globalData.url + 'Hxorder',
            method: 'POST',
            data: {
              oid: ercode.oid,
              uid: app.globalData.uid,
            },
            success: function (res) {
              if (res.data.info) {
                wx.hideLoading()
                that.getIndex()
                wx.showToast({
                  title: '核销成功',
                  duration: 1200,
                  icon: "loading",
                  mask: true,
                  success: (res) => {
                    setTimeout(() => {
                      that.getIndex()
                    }, 1300)
                  },
                })
              } else {
                wx.showToast({
                  title: res.data.msg,
                  duration: 1200,
                  icon: "loading",
                  mask: true,
                  success: (res) => {},
                })
              }
            },
          })
        }
      },
      fail: (res) => {
        wx.showToast({
          title: res.data.msg,
          duration: 1200,
          icon: "loading",
          mask: true,
          success: (res) => {},
        })
      },
    })
    wx.hideLoading()
  },

  getIndex() {
    var that = this;
    wx.showLoading({
      title: '玩命加载中',
      mask: true,
    })
    wx.request({
      url: app.globalData.url + 'Hxpage',
      method: 'POST',
      data: {
        oid: that.data.oid,
        hx_uid: app.globalData.uid,
      },
      success: function (res) {
        if (res.data.info) {
          that.setData({
            info: res.data.info,
            goods: res.data.info.goods,
            store: res.data.info.store,
            status: res.data.info.orders.status,
          })
        }
        wx.hideLoading()
      },
    })
  },

  bindContinue(e) {
    const that = this;
    wx.scanCode({
      success(res) {
        var str = res.path;
        var string =  str.slice(30)
        let code = that.getScene(string);
        wx.showLoading({
          title: '玩命加载中',
          mask: true,
        })
        wx.request({
          url: app.globalData.url + 'Hxpage',
          method: 'POST',
          data: {
            oid: code.oid,
            hx_uid: app.globalData.uid,
          },
          success: function (res) {
            if (res.data.info) {
              that.setData({
                info: res.data.info,
                goods: res.data.info.goods,
                store: res.data.info.store,
                status: res.data.info.orders.status,
              })
            }
            wx.hideLoading()
          },
        })
      }
    })
  },

  getScene: function (scene) {
    if (scene == "") return {}
    let res = {}
    let params = decodeURIComponent(scene).split("&")
    params.forEach(item => {
      let pram = item.split("=")
      res[pram[0]] = pram[1]
    })
    return res
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    let ercode = that.getScene(options.scene);
    that.setData({
      oid: ercode.oid,
    })
    if (app.globalData.uid) {
      needUid();
    } else {
      app.idCB = function (res) {
        needUid();
      }
    }
    wx.stopPullDownRefresh();

    function needUid() {
      wx.showLoading({
        title: '玩命加载中',
        mask: true,
      })
      wx.request({
        url: app.globalData.url + 'Hxpage',
        method: 'POST',
        data: {
          oid: ercode.oid,
          hx_uid: app.globalData.uid,
        },
        success: function (res) {
          if (res.data.info == false) {
            wx.showToast({
              title: res.data.msg,
              duration: 1500,
              icon: "loading",
              mask: true,
              success: (res) => {
                setTimeout(() => {
                  wx.switchTab({
                    url: '../index/index'
                  })
                }, 1500)
              },
            })
            return
          } else {
            that.setData({
              info: res.data.info,
              goods: res.data.info.goods,
              store: res.data.info.store,
              status: res.data.info.orders.status,
            })
          }
          wx.hideLoading()
        },
      })
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onLoad();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})